/*
 * NmeaParser.h
 *
 *  Created on: 27.6.2011.
 *      Author: Hetcheem
 */

#ifndef NMEA_PARSER_H_
#define NMEA_PARSER_H_

#include "GPSData.h"
#include "DTWX3Data.h"
#include "PCX03Data.h"
#include "AVX03Data.h"

#include <stdio.h>
#include <string>

static const unsigned NMEA_SEQUENCE_MAX_LENGTH = 81;
static const unsigned ADDRESS_FIELD_MAX_LENGTH = 10;

/*
 * Incorporates singleton pattern
 * */
class NmeaParser
{
	enum NMEAParserState {
		SearchForStartOfSentence = 1,	// Start of sentence
		RetrieveSentence,
		GetChecksumCharacters,
		WaitForSentenceTermination
	};

private:
	NMEAParserState m_state;
	unsigned m_calcChecksum;
	char m_checksumStr[3];
	int m_checksumIndex;
	bool m_waitingFirstChecksumCharacter;
	char m_NMEASequence[NMEA_SEQUENCE_MAX_LENGTH];
	unsigned m_NMEASequenceIndex;
	
	static NmeaParser* pInstance;

    // Private constructor prevents instantiation from other classes
	NmeaParser();
    virtual ~NmeaParser() {}

public:
	static NmeaParser* GetParser();
	void Parse(const char *buffer, const unsigned buffSize);

private:
	int ParseNMEASentence(char *buffer);
    
    int isxdigit(const int& c);
    int axtoi(const char *str);
    
////////////////////////////////////////////////////////
#ifdef DTWX3DATA_H_
private:
    DTWX3Data m_DTWX3Data;
public:
    DTWX3Data& GetDTWX3Data()  { return m_DTWX3Data; }
#endif /* DTWX3DATA_H_ */

////////////////////////////////////////////////////////
#ifdef GPSDATA_H_
private:
    GPSData m_GPSData;
public:
    GPSData& GetGPSData() { return m_GPSData; }
#endif /* GPSDATA_H_ */

////////////////////////////////////////////////////////
#ifdef AVX03DATA_H_
private:
    AVX03Data m_AVX03Data;
public:
    AVX03Data& GetAVX03Data() { return m_AVX03Data; }
#endif /* AVX03DATA_H_ */
    
////////////////////////////////////////////////////////
#ifdef PCX03DATA_H_
private:
    PCX03Data m_PCX03Data;
public:
    PCX03Data& GetPCX03Data() { return m_PCX03Data; }
#endif /* PCX03DATA_H_ */
};

#endif /* NMEA_PARSER_H_ */
